import { loginAPI, getInfoAPI, getUserInfoAPI } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: () => {
    return {
      token: getToken() || '',
      userInfo: {}
    }
  },
  mutations: {
    setToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    setUserInfo(state, info) {
      state.userInfo = info
    }
  },
  actions: {
    // 登录
    async loginAPI(context, data) {
      const res = await loginAPI(data)
      // console.log(res)
      context.commit('setToken', res.data)
    },
    // 获取用户信息
    async getInfoAPI(context) {
      // 获取基本档案(无头像)
      const res = await getInfoAPI()
      // console.log(res)
      // 获取基本信息(有头像)
      const resInfo = await getUserInfoAPI(res.data.userId)
      // console.log(resInfo)
      context.commit('setUserInfo', { ...res.data, ...resInfo.data })
    },
    delInfo(context) {
      context.commit('setToken', '')
      context.commit('setUserInfo', {})
    }
  },
  getters: {}
}
